package de.bastian.hinterleitner.adaptersviews;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import de.bastian.hinterleitner.database.DataHelper;
import de.bastian.hinterleitner.tools.Constants;
import de.bastian.hinterleitner.tools.Entry;
import de.bastian.hinterleitner.tools.Helper;
import de.bastian.hinterleitner.tools.Unit;
import de.hijacksoft.wyc.R;

/**
 *
 * @author basti
 */
public class StandardAdapter extends BasicAdapter{

	/**
	 * change this String to mark a substring in the name of the entry fat!
	 */
	public String fatString = "";
    
    public StandardAdapter(Context c, ArrayList<Entry> e){
        entries = e;
        context = c;
    }

    public View getView(int i, View view, ViewGroup vg) {
        if(entries.get(i).getId()==Constants.ID_SEPERATOR){
            LayoutInflater inflator = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflator.inflate(R.layout.list_listseperator, null);
        	((TextView)view.findViewById(R.id.list_text_name)).setText(entries.get(i).getName());
        }else{
            LayoutInflater inflator = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflator.inflate(R.layout.list_defaultlist, null);
	        
	        double val = Math.rint(100*entries.get(i).getValue())/100;
	        
	        ImageView icon = ((ImageView)view.findViewById(R.id.list_image_icon));
	        ImageView repeat = ((ImageView)view.findViewById(R.id.list_image_repeat));
	        
	        DataHelper dh = DataHelper.getDataHelper(context);
	        
	    	icon.setImageDrawable(dh.getCategoryDrawable(entries.get(i).getCategory().toString()));
	    	if(entries.get(i).getRepeatableReference() != -1){
	    		repeat.setVisibility(View.VISIBLE);
	    	}else{
	    		repeat.setVisibility(View.GONE);
	    	}
	    	
	        TextView value = ((TextView)view.findViewById(R.id.list_text_value));
	        TextView date = ((TextView)view.findViewById(R.id.list_text_date));
	        TextView nameView = ((TextView)view.findViewById(R.id.list_text_name));
	        
	        //make the name fat at a given String
	        String name = entries.get(i).getName();
	        if(fatString.length()>0){
		        String upperName = name.toUpperCase();
		        fatString = fatString.toUpperCase();
		        int start = upperName.indexOf(fatString);
		        int end = start+fatString.length();
		        String newName = "";
		        for(int index=0;index<start; index++){
		        	newName += name.charAt(index);
		        }
		        newName += "<b>";
				for(int index=start;index<end; index++){
					newName += name.charAt(index);   	
			    }
				newName += "</b>";
				for(int index=end;index<name.length(); index++){
					newName += name.charAt(index);
				}
				name = newName;
	        }
	        
	        nameView.setText(Html.fromHtml(name));
	        
	        if(entries.get(i).getId()!=Constants.ID_NODATE){
	        	Calendar c = Calendar.getInstance();
	        	c.setTime(entries.get(i).getDate());
	        	int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
	        	String extra = getDistance(entries.get(i).getDate());
	        	String day = extra+context.getResources().getStringArray(R.array.days)[dayOfWeek-1];
	            date.setText(day+", "+entries.get(i).getDay()+"."+entries.get(i).getMonth()+"."+entries.get(i).getYear());
	            // disable future entries
	            if(new Date().before(entries.get(i).getDate())){
	            	date.setTextColor(Color.LTGRAY);
	            	nameView.setTextColor(Color.LTGRAY);
	            }else{
	            	date.setTextColor(Color.BLACK);
	            	nameView.setTextColor(Color.BLACK);
	            }
	        }else{
	            date.setText(context.getString(R.string.clickfordetails));
	            icon.setImageDrawable(context.getResources().getDrawable(R.drawable.hijackcalendar));
	        }
	        
	        String unit = context.getString(R.string.currency);
	        
	        Unit u = entries.get(i).getUnit();
	        if(u != null){
	        	unit = u.getSign();
	        }
	        
	        value.setText(Helper.getValString(val)+" "+unit);
	        
	        if(val<0){
	            value.setTextColor(0xffdd3333);
	        }else{
	            value.setTextColor(0xff33aa33);
	        }
        }
        return view;
    }

	private String getDistance(Date date) {
		long oneDay = 86400l;
		Date nowDate = new Date();
		nowDate.setHours(0);
		nowDate.setMinutes(0);
		nowDate.setSeconds(0);
		long now = nowDate.getTime()/1000;
		long then = date.getTime()/1000;
		if(now+oneDay<=then&&now+oneDay+oneDay>then){
			return context.getString(R.string.tomorrow)+", ";
		}else if(now>then&&now-oneDay<=then){
			return context.getString(R.string.yesterday)+", ";
		}else if(now<=then&&now+oneDay>=then){
			return context.getString(R.string.today)+", ";
		}else{
			return "";
		}
	}
}
